"use strict";
import createRecycleContext from "../../compoment/virtualList/index";
import { getMyOrder } from "../../utils/order"
import { createUser } from "../../utils/user"
Page({
    data: {
        numbers: [1, 2, 3, 4],
        activeTab: 0,
        canGetUser: false,
        initList: []
    },
    changeTab: function (e) {
        const {
            target: {
                id
            }
        } = e;
        wx.showLoading({
            title: 'loading',
            mask: true
        })
        let activeNum = 0;
        switch (id) {
            case "await":
                activeNum = 0; break;
            case "finish":
                activeNum = 1; break;
            case "expried":
                activeNum = 2; break;
            case "completed":
                activeNum = 3; break;
            default:
                return null
        }
        this.setData({ activeTab: activeNum });
        getMyOrder(() => {
            setTimeout(() => {
                wx.hideLoading()
            }, 500)
        }, { status: activeNum })
    },
    scroll: function () {

    },

    onLoad: function () {

    },
    updateList(data, scrollTop = 0,) {
        var ctx = createRecycleContext({
            id: 'recycleId',
            dataKey: 'recycleList1',
            page: this,
            itemSize: {
                width: 365,
                height: 153
            }
        })
        const query = wx.createSelectorQuery();
        query.select("#recycleId").boundingClientRect(rect => {
            const { height } = rect;
            const count = Math.ceil(height * 2 / 365)
            const start = Math.floor(scrollTop * 2 / 365);
            const end = start + count + 1;
            ctx.append(data.splice(start, end))
        }).exec()
    },
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
        const canGetUser = wx.getStorageSync("canGetUser");
        if (canGetUser) {
            this.setData({
                canGetUser: true,
            })
            wx.showLoading({
                title: 'loading',
                mask: true
            })
            getMyOrder(({
                data
            }) => {
                this.setData({
                    initList: data
                })
                this.updateList(data,0)
                wx.hideLoading()
            }, { status: 0 });
        }
    },
    getUserProfile: function () {
        const that = this;
        wx.getUserProfile({
            desc: '展示用户信息',
            success: function (res) {
                wx.showLoading({
                    title: 'loading',
                    mask: true
                })
                wx.setStorageSync('user', res.userInfo)
                wx.setStorageSync('canGetUser', true)
                createUser(res.userInfo, ({
                    data
                }) => {
                    wx.setStorageSync('token', data.token)
                    getMyOrder(({
                        data
                    }) => {
                        that.setData({
                            initList: data,
                        })
                        that.updateList(0, data)
                        wx.hideLoading()
                    });
                })

                that.setData({
                    userInfo: res.userInfo,
                    canGetUser: true
                });
            }
        });
    }
});